﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Web;

    public class Account
    {
        private readonly KeRui.DAL.Account dal = new KeRui.DAL.Account();
        public string RealName = "";
        public int UserID = -1;
        public string UserName = "";

        public int Add(KeRui.Model.Account model)
        {
            return this.dal.Add(model);
        }

        public int AddNoAdmin(KeRui.Model.Account model)
        {
            return this.dal.AddNoAdmin(model);
        }

        public int CheckLogin(int UserID, string Password)
        {
            return this.dal.CheckLogin(UserID, Password);
        }

        public int CheckLogin(string Username, string Password)
        {
            return this.dal.CheckLogin(Username, Password);
        }

        public void Delete(int UserID)
        {
            this.dal.Delete(UserID);
        }

        private static int GetCookieUserID(bool IsAdmin)
        {
            KeRui.BLL.SysIpBlock block;
            if (Utils.GetCookie(ConfigHelper.GetConfigString("CookieName")) == "")
            {
                Cookie.ClearUserCookie();
                return -1;
            }
            int userID = Utils.StrToInt(Cookie.GetCookie(ConfigHelper.GetConfigString("CookieUserid")), -1);
            string cookiePassword = Cookie.GetCookiePassword(Cookie.GetCookie(ConfigHelper.GetConfigString("CookiePassword")));
            if ((userID == -1) || (cookiePassword == ""))
            {
                Cookie.ClearUserCookie();
                return -1;
            }
            switch (KeRui.BLL.SysConfig.GetInt("WebConfig", "AdminIpBlockType", 0))
            {
                case 1:
                    block = new KeRui.BLL.SysIpBlock();
                    if (block.Exists(0, 1, Utils.GetRealIP()))
                    {
                        return -1;
                    }
                    break;

                case 2:
                    block = new KeRui.BLL.SysIpBlock();
                    if (!block.Exists(0, 0, Utils.GetRealIP()))
                    {
                        return -1;
                    }
                    break;
            }
            int num3 = new KeRui.BLL.Account().CheckLogin(userID, cookiePassword);
            if (num3 < 1)
            {
                Cookie.ClearUserCookie();
                return -1;
            }
            KeRui.Model.Member model = new KeRui.BLL.Member().GetModel(num3);
            if (model == null)
            {
                Cookie.ClearUserCookie();
                return -1;
            }
            if (model.State == 0)
            {
                Cookie.ClearUserCookie();
                return -1;
            }
            if (IsAdmin)
            {
                KeRui.Model.Admin admin2 = new KeRui.BLL.Admin().GetModel(num3);
                if (admin2 == null)
                {
                    Cookie.ClearUserCookie();
                    return -1;
                }
                if (admin2.State == 0)
                {
                    Cookie.ClearUserCookie();
                    return -1;
                }
            }
            return num3;
        }

        public static int GetLoginAdminID()
        {
            int cookieUserID = -1;
            if (HttpContext.Current.Session["adminid"] == null)
            {
                cookieUserID = GetCookieUserID(true);
                if (cookieUserID > 0)
                {
                    HttpContext.Current.Session["adminid"] = cookieUserID.ToString();
                }
                return cookieUserID;
            }
            cookieUserID = Utils.StrToInt(HttpContext.Current.Session["adminid"].ToString(), -1);
            if (cookieUserID <= 0)
            {
                HttpContext.Current.Session.Remove("adminid");
            }
            return cookieUserID;
        }

        public static int GetLoginUserID()
        {
            int cookieUserID = -1;
            if (HttpContext.Current.Session["userid"] == null)
            {
                cookieUserID = GetCookieUserID(false);
                if (cookieUserID > 0)
                {
                    HttpContext.Current.Session["userid"] = cookieUserID.ToString();
                }
                return cookieUserID;
            }
            cookieUserID = Utils.StrToInt(HttpContext.Current.Session["userid"].ToString(), -1);
            if (cookieUserID <= 0)
            {
                HttpContext.Current.Session.Remove("userid");
            }
            return cookieUserID;
        }

        public KeRui.Model.Account GetModel(int UserID)
        {
            return this.dal.GetModel(UserID);
        }

        public KeRui.Model.Account GetModel(string Username)
        {
            return this.dal.GetModel(Username);
        }

        public bool Q_ChkLogin()
        {
            HttpRequest request = HttpContext.Current.Request;
            HttpResponse response = HttpContext.Current.Response;
            HttpServerUtility server = HttpContext.Current.Server;
            bool flag = true;
            if (((HttpContext.Current.Session["userID"] == null) || (HttpContext.Current.Session["userID"] == "")) || (HttpContext.Current.Session["UserName"] == null))
            {
                HttpCookie cookie = request.Cookies["QuserLogin"];
                if (cookie == null)
                {
                    return false;
                }
                if (cookie.Values.Count == 0)
                {
                    return false;
                }
                try
                {
                    if (this.CheckLogin(Convert.ToInt32(cookie["UserID"].ToString()), cookie["password"].ToString()) <= 0)
                    {
                        return false;
                    }
                    KeRui.Model.Member model = new KeRui.BLL.Member().GetModel(Convert.ToInt32(cookie["UserID"].ToString()));
                    if (model == null)
                    {
                        return false;
                    }
                    if (model.State == 0)
                    {
                        return false;
                    }
                    HttpContext.Current.Session["UserID"] = model.UserID.ToString();
                    HttpContext.Current.Session["UserName"] = model.Username;
                    HttpContext.Current.Session["RealName"] = server.UrlDecode(model.RealName);
                    this.UserID = model.UserID;
                    this.UserName = model.Username;
                    this.RealName = server.UrlDecode(model.RealName);
                    return flag;
                }
                catch
                {
                    flag = false;
                }
                return flag;
            }
            this.UserID = Convert.ToInt32(HttpContext.Current.Session["UserID"].ToString());
            this.UserName = HttpContext.Current.Session["UserName"].ToString();
            this.RealName = HttpContext.Current.Session["RealName"].ToString();
            return flag;
        }

        public int Update(KeRui.Model.Account model)
        {
            return this.dal.Update(model, "");
        }

        public int Update(KeRui.Model.Account model, string oldPwd)
        {
            return this.dal.Update(model, oldPwd);
        }
    }
}

